package com.wavehi.request_mapping.config;

import com.wavehi.request_mapping.pojo.Pet;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.util.UrlPathHelper;
import org.thymeleaf.util.StringUtils;

/**
 * @Author WaveHi
 * @Date 4/11/2021 下午 5:26
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description
 */
@Configuration
public class MyWebConfig implements WebMvcConfigurer {

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        UrlPathHelper urlPathHelper = new UrlPathHelper();
        urlPathHelper.setRemoveSemicolonContent(false);
        configurer.setUrlPathHelper(urlPathHelper);
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new Converter<String, Pet>() {
            @Override
            public Pet convert(String s) {
                Pet pet = new Pet();
                if(!StringUtils.isEmpty(s)) {
                    String[] split = s.split(",");
                    pet.setName(split[0]);
                    pet.setBrand(split[1]);
                }
                return pet;
            }
        });
    }
}
